<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 头部 -->
        <PageTools :show-before="false">
          <template #after>
            <el-button type="primary" size="small" @click="addFn('0','1')">添加权限</el-button>
          </template>
        </PageTools>
        <!-- 表格 -->
        <el-table border :data="perrsionsList" row-key="id">
          <el-table-column label="权限名称" prop="title" />
          <el-table-column label="权限标识" prop="code" />
          <el-table-column label="权限描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.pid==='0'" size="small" type="primary" @click="addFn(String(row.id),'2')">添加子权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <addPerm ref="addPerm" :show-dialog.sync="visableDialog" @updateEvent="getPermissionsList" />
    </div>
  </div>
</template>

<script>
import { getPermissions, getPermissionsList, deletePermissions } from '@/api/permissions'
import { listToTree } from '@/utils/index'
import addPerm from './components/add-perm.vue'
export default {
  components: {
    addPerm
  },
  data() {
    return {
      perrsionsList: [],
      // total: 1, // 总数量
      params: {
        _limit: 2,
        _start: 0
      },
      visableDialog: false
    }
  },
  created() {
    this.getPermissionsList()
  },
  methods: {
    // 点击删除
    delFn(id) {
      this.$confirm('您确定删除这个权限吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        await deletePermissions(id)
        this.getPermissionsList()
      }).catch(() => {
        console.log('取消')
      })
    },
    // 点击编辑
    editFn(id) {
      this.visableDialog = true
      this.$refs.addPerm.getPermissionsId(id)
    },
    // 点击添加权限事件
    addFn(pid, type) {
      this.visableDialog = true
      this.$refs.addPerm.formData.pid = pid
      this.$refs.addPerm.formData.type = type
    },
    // 获取权限数组列表
    async getPermissionsList() {
      const res = await getPermissions()
      // console.log(44, res)
      this.total = res.data
      console.log(this._limit)
      const val = await getPermissionsList(this.parmas)
      console.log(52, val)
      this.perrsionsList = listToTree(val.data, 0)
    }
  }
}
</script>

<style>

</style>
